﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuVien;
using System.Data;
using System.Web.Services;
using System.Web.Script.Services;

namespace TranTekBE.NhaThuoc
{
    public partial class Thong_Bao_Thuoc_Dan_Sai_Ma_Code : PageRequireLogin
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Load JS
            ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "LoadJS", "LoadJS();", true);
            if (Request.QueryString["product"] == null && Request.QueryString["user"] == null && Request.QueryString["store"] == null && Request.QueryString["time"] == null)
            {
                LoadData("", "", "", "");
            }
            else
            {
                LoadData(Request.QueryString["product"], Request.QueryString["user"], Request.QueryString["store"], Request.QueryString["time"]);
            }
            if (!IsPostBack)
            {
                lbStore.Text = UserInfo.Stores;
                employee.Text = UserInfo.LastName + " " + UserInfo.FirstName;
                //
                // Get user
                FunctionsUtils.GetDropListUser(droplist_loc_User);
                // Get stores
                FunctionsUtils.GetDropListStore(droplist_cua_hang_loc_kq);
                //
                if (Request.QueryString["product"] == null && Request.QueryString["user"] == null && Request.QueryString["store"] == null && Request.QueryString["time"] == null)
                {
                    droplist_cua_hang_loc_kq.SelectedValue = UserInfo.Stores;
                }
                else
                {
                    string idsp = Request.QueryString["product"];
                    DataSet ds = FunctionsUtils.Database.ExecuteQuery("SELECT C001200 as ID, C001203 as POS, C001204 as Name FROM T0012 WHERE C001200 = '" + idsp + "'");
                    if (ds != null)
                    {
                        foreach (DataRow Rows in ds.Tables[0].Rows)
                        {
                            txtTenSP.Text = Rows["Name"].ToString();
                        }
                    }
                    droplist_loc_User.SelectedValue = Request.QueryString["user"];
                    droplist_cua_hang_loc_kq.SelectedValue = Request.QueryString["store"];
                    txt_thoi_gian_loc.Text = Request.QueryString["time"];
                }
                
            }
        }
        

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<AutocompleteModel> getnameserver(string value)
        {
            List<AutocompleteModel> list = new List<AutocompleteModel>();



            List<string> abc = new List<string>();
            DataSet searchlist = FunctionsUtils.Database.ExecuteQuery("SELECT TOP 10 C001200 Id,C001204 Name FROM T0012 WHERE C001204 LIKE '%" + value + "%'");
            if (searchlist != null)
            {
                foreach (DataRow Rows in searchlist.Tables[0].Rows)
                {
                    //abc.Add(Rows[1].ToString());
                    list.Add(new AutocompleteModel() { value = Rows["Id"].ToString(), label = Rows["Name"].ToString() });
                }
            }
            return list;
        }
        protected void submit_Click(object sender, EventArgs e)
        {
            string IDthuoc = "-1";
            string MaPOS = "-1";
            string CodeSai = txtMaCode.Text.ToString().Replace("'", "''");
            string Tenthuoc = tbAuto.Text.ToString().Replace("'", "''");
            
            string IDnguoigui = UserInfo.UserID;
            string store_user = UserInfo.Stores;
            
            string datenow = DateTime.Now.ToString();
            
            string IDSanPham = Request.Form["idsp"];

            /*
            * Kiểm tra xem thuốc đã có trong database hay chưa
            */
            try
            {
                string sql = "SELECT C001200 as ID, C001203 as POS, C001204 as Name FROM T0012 WHERE C001200 = '" + IDSanPham + "'";
                DataSet checkidthuoc = FunctionsUtils.Database.ExecuteQuery(sql);
                if (checkidthuoc != null)
                {
                    foreach (DataRow Rows in checkidthuoc.Tables[0].Rows)
                    {
                        IDthuoc = Rows["ID"].ToString();
                        MaPOS = Rows["POS"].ToString();
                    }
                }
            }
            catch (Exception err)
            {
                status.Text = err.Message;
            }

            if (Tenthuoc.Length > 0 && txtMaCode.Text != null && txtMaCode.Text != "")
            {
                string sql = "INSERT INTO tbl_SPSaiMaCode VALUES (N'" + CodeSai + "',N'" + IDthuoc + "',N'" + MaPOS + "',N'" + Tenthuoc + "',N'" + IDnguoigui + "',N'" + store_user + "',N'" + datenow + "')";
                try
                {
                    FunctionsUtils.Database2.ExecuteNonQuery(sql);
                    Response.Redirect("Thong-Bao-Thuoc-Dan-Sai-Ma-Code.aspx");

                }
                catch (Exception err)
                {
                    status.Text = err.Message;
                }
            }
            else
            {
                status.Text = "<font color='red'>Nhập thiếu thông tin, vui lòng kiểm tra lại!</font></br>";
            }

        }
        // Load danh sách sản phẩm
        void LoadData(string getSP, string getUser, string getStore, string getTime)
        {
            string sql_loc = @"
            SELECT CodeSai, MaPOS,TenThuoc,IDNguoiGui,CuaHangGui,ThoiGianGui, b.HO_USER +' '+ b.TEN_USER as HoTen
            FROM tbl_SPSaiMaCode as a
            Left Join tbl_UserKPI as b on b.ID_USER = a.IDNguoiGui
            where 1=1
            ";
            //

            string sql_getSP = "";
            string sql_getUser = "";
            string sql_getStore = "";
            string sql_getTime = "";

            if (getSP != "" && getSP != null)
            {
                sql_getSP = " AND IDThuoc = '" + getSP + "'";
            }

            if (getUser != "" && getUser != null)
            {
                sql_getUser = " AND IDNguoiGui = '" + getUser + "'";
            }
            if (getStore != "" && getStore != null)
            {
                sql_getStore = " AND CuaHangGui = '" + getStore + "'";
            }
            if (getTime != "" && getTime != null)
            {
                sql_getTime = " AND ThoiGianGui = '" + getTime + "'";
            }


            sql_loc += sql_getSP + sql_getUser + sql_getStore + sql_getTime;
            //
            DataSet ds = FunctionsUtils.Database2.ExecuteQuery(sql_loc);

            DataTable dt = ds.Tables[0];

            PagedDataSource pgitems = new PagedDataSource();

            System.Data.DataView dv = new System.Data.DataView(dt);

            pgitems.DataSource = dv;

            pgitems.AllowPaging = true;

            pgitems.PageSize = 10;

            pgitems.CurrentPageIndex = PageNumber;

            if (pgitems.PageCount > 1)
            {

                rptPages.Visible = true;

                System.Collections.ArrayList pages = new System.Collections.ArrayList();

                for (int i = 0; i < pgitems.PageCount; i++)

                    pages.Add((i + 1).ToString());

                rptPages.DataSource = pages;

                rptPages.DataBind();

            }

            else

            { rptPages.Visible = false; }

            //

            rpSanPham.DataSource = pgitems;

            rpSanPham.DataBind();
        }

        public int PageNumber
        {

            get
            {

                if (ViewState["PageNumber"] != null)

                    return Convert.ToInt32(ViewState["PageNumber"]);

                else

                    return 0;

            }

            set
            {

                ViewState["PageNumber"] = value;

            }

        }

        protected void rptPages_ItemCommandSP(object source, RepeaterCommandEventArgs e)
        {

            PageNumber = Convert.ToInt32(e.CommandArgument) - 1;

            if (Request.QueryString["product"] == null && Request.QueryString["user"] == null && Request.QueryString["store"] == null && Request.QueryString["time"] == null)
            {
                LoadData("", "", "", "");
            }
            else
            {
                LoadData(Request.QueryString["product"], Request.QueryString["user"], Request.QueryString["store"], Request.QueryString["time"]);
            }

        }



        protected void txtTenSP_TextChanged(object sender, EventArgs e)
        {
            Response.Redirect("Thong-Bao-Thuoc-Dan-Sai-Ma-Code.aspx?product=" + Request.Form["loc-idsp"].ToString() + "&user=" + droplist_loc_User.SelectedValue.ToString() + "&store=" + droplist_cua_hang_loc_kq.SelectedValue.ToString() + "&time=" + txt_thoi_gian_loc.Text.ToString());
        }

        protected void droplist_loc_User_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("Thong-Bao-Thuoc-Dan-Sai-Ma-Code.aspx?product=" + Request.Form["loc-idsp"].ToString() + "&user=" + droplist_loc_User.SelectedValue.ToString() + "&store=" + droplist_cua_hang_loc_kq.SelectedValue.ToString() + "&time=" + txt_thoi_gian_loc.Text.ToString());
        }

        protected void droplist_cua_hang_loc_kq_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("Thong-Bao-Thuoc-Dan-Sai-Ma-Code.aspx?product=" + Request.Form["loc-idsp"].ToString() + "&user=" + droplist_loc_User.SelectedValue.ToString() + "&store=" + droplist_cua_hang_loc_kq.SelectedValue.ToString() + "&time=" + txt_thoi_gian_loc.Text.ToString());
        }

        protected void txt_thoi_gian_loc_TextChanged(object sender, EventArgs e)
        {
            Response.Redirect("Thong-Bao-Thuoc-Dan-Sai-Ma-Code.aspx?product=" + Request.Form["loc-idsp"].ToString() + "&user=" + droplist_loc_User.SelectedValue.ToString() + "&store=" + droplist_cua_hang_loc_kq.SelectedValue.ToString() + "&time=" + txt_thoi_gian_loc.Text.ToString());
        }

        protected void btnLoc_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["product"] == null && Request.QueryString["user"] == null && Request.QueryString["store"] == null && Request.QueryString["time"] == null)
            {
                LoadData("", "", "", "");
            }
            else
            {
                LoadData(Request.QueryString["product"], Request.QueryString["user"], Request.QueryString["store"], Request.QueryString["time"]);
            }
        }

    }
}